home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / base / models.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  5.7 KB  |  181 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20.  
  21. from base.g import *
  22. import os.path, re, glob
  23.  
  24. class ModelData:
  25.     def __init__(self, root_path=None):
  26.         if root_path is None:
  27.             self.root_path = prop.models_dir
  28.         else:
  29.             self.root_path = root_path
  30.  
  31.         self.__cache = {}
  32.         self.reset_includes()
  33.         self.sec = re.compile(r'^\[(.*)\]')
  34.         self.inc = re.compile(r'^\%include (.*)', re.IGNORECASE)
  35.         self.inc_line = re.compile(r'^\%(.*)\%')
  36.         self.eq = re.compile(r'^([^=]+)=(.*)')
  37.  
  38.     def read_all_files(self, unreleased=True):
  39.         released_dat = os.path.join(self.root_path, "models.dat")
  40.         log.debug("Reading file: %s" % released_dat)
  41.         self.read_section(released_dat)
  42.  
  43.         unreleased_dir = os.path.join(self.root_path, 'unreleased')
  44.         if unreleased and os.path.exists(unreleased_dir):
  45.             unreleased_dat = os.path.join(self.root_path, "unreleased", "unreleased.dat")
  46.             log.debug("Reading file: %s" % unreleased_dat)
  47.             self.read_section(unreleased_dat)
  48.  
  49.         return self.__cache
  50.  
  51.     def read_section(self, filename, section=None, is_include=False): # section==None, read all sections
  52.         found, in_section = False, False
  53.  
  54.         if section is not None:
  55.             section = section.lower()
  56.  
  57.             if is_include:
  58.                 log.debug("Searching for include [%s] in file %s" % (section, filename))
  59.             else:
  60.                 log.debug("Searching for section [%s] in file %s" % (section, filename))
  61.  
  62.         if is_include:
  63.             cache = self.__includes
  64.         else:
  65.             cache = self.__cache
  66.  
  67.         try:
  68.             fd = file(filename)
  69.         except IOError, e:
  70.             log.error("I/O Error: %s (%s)" % (filename, e.strerror))
  71.             return False
  72.  
  73.         while True:
  74.             line = fd.readline()
  75.  
  76.             if not line:
  77.                 break
  78.  
  79.             if line[0] in ('#', ';'):
  80.                 continue
  81.  
  82.             if line[0] == '[':
  83.                 if in_section and section is not None:
  84.                     break
  85.  
  86.                 match = self.sec.search(line)
  87.  
  88.                 if match is not None:
  89.                     in_section = True
  90.  
  91.                     read_section = match.group(1).lower()
  92.  
  93.                     if section is not None:
  94.                         found = in_section = (read_section == section)
  95.  
  96.                     if in_section:
  97.                         if section is not None:
  98.                             log.debug("Found section [%s] in file %s" % (read_section, filename))
  99.  
  100.                         cache[read_section] = {}
  101.  
  102.                 continue
  103.  
  104.             if line[0] == '%':
  105.                 match = self.inc.match(line)
  106.  
  107.                 if match is not None:
  108.                     inc_file = match.group(1)
  109.                     log.debug("Found include file directive: %%include %s" % inc_file)
  110.                     self.__include_files.append(os.path.join(os.path.dirname(filename), inc_file))
  111.                     continue
  112.  
  113.                 if in_section:
  114.                     match = self.inc_line.match(line)
  115.  
  116.                     if match is not None: 
  117.                         inc_sect = match.group(1)
  118.                         log.debug("Found include directive %%%s%%" % inc_sect)
  119.  
  120.                         try:
  121.                             self.__includes[inc_sect]
  122.                         except KeyError:
  123.                             for inc in self.__include_files:
  124.  
  125.                                 if self.read_section(inc, inc_sect, True):
  126.                                     break
  127.                             else:
  128.                                 log.error("Include %%%s%% not found." % inc_sect)
  129.  
  130.  
  131.             if in_section:
  132.                 match = self.eq.search(line)
  133.  
  134.                 if match is not None:
  135.                     value = match.group(2)
  136.  
  137.                     try:
  138.                         value = int(value)
  139.                     except ValueError:
  140.                         pass
  141.  
  142.                     cache[read_section][match.group(1)] = value
  143.  
  144.         fd.close()
  145.         return found
  146.  
  147.     def reset_includes(self):
  148.         self.__include_files = []
  149.         self.__includes = {}
  150.  
  151.  
  152.     def __getitem__(self, model):
  153.         model = model.lower()
  154.  
  155.         try:
  156.             return self.__cache[model]
  157.         except:
  158.             log.debug("Cache miss: %s" % model)
  159.  
  160.             released_dat = os.path.join(self.root_path, "models.dat")
  161.             log.debug("Reading file: %s" % released_dat)
  162.  
  163.             if self.read_section(released_dat, model):
  164.                 return self.__cache[model]
  165.  
  166.             unreleased_dir = os.path.join(self.root_path, 'unreleased')
  167.  
  168.             if os.path.exists(unreleased_dir):
  169.                 unreleased_dat = os.path.join(self.root_path, "unreleased", "unreleased.dat")
  170.                 log.debug("Reading file: %s" % unreleased_dat)
  171.  
  172.                 if self.read_section(unreleased_dat, model):
  173.                     return self.__cache[model]
  174.  
  175.             return {}
  176.  
  177.  
  178.     def all_models(self):
  179.         return self.__cache
  180.  
  181.